<?php 
namespace app\models;
use Yii;
use app\models\Comment;
use app\models\Comment_live;
use app\models\User;
use app\models\Article;
use app\models\Live;
use Yii\db\Connection;
use yii\helpers\Url;
// xiong类为一些自定义的一些方法
class Xiong 
{
    // give up
    // public static function getArticleUrl($post_name)
    // {
    //     // $path =dirname(dirname(dirname(__FILE__)))."/frontend/web/index.php";

    //     $path =Url::toRoute(['blog/index']).'$article='.$post_name;
    //     return $path;       
    // }
    //   // give up
    // public static function getLiveUrl($post_name)
    // {
    //     // $path =dirname(dirname(dirname(__FILE__)))."/frontend/web/index.php";
    //        $path =Url::toRoute(['blog/live']).'$article='.$post_name;
    //     return $path;       
    // }
    /*
	1,userid = 0时说明此评论是游客评论的。
    */
    // public static function getArtComm($id)
    // {
    //     $com = Comment::find()->where(['article_id'=>$id])->all();
    //     $data='';
    //     if($com!=null){
    //         foreach ($com as $key => $value) {
    //             $data[$key]['content'] =$value->comment_content;
    //             $data[$key]['date'] =$value->comment_date;
                
    //     	}
    //     }
    //     return $data;

    // }
    // public static function getLiveComm($id)
    // {
        
    //     $com = Comment_live::find()->where(['live_id'=>$id])->all();
    //     $data='';
    //     if($com!=null){
    //         foreach ($com as $key => $value) {
    //             $data[$key]['content'] =$value->comment_content;
    //             $data[$key]['date'] =$value->comment_date;
    //             $user = User::find()->where(['id'=> $value->user_id])->one();
    //             if($user!=null)
    //             $data[$key]['username'] =$user->username;
    //             else
    //             $data[$key]['username'] ="游客";
    //         }
    //     }
    //     return $data;

    // }
    /*
    删除img,(根据url删除)
    */
public function rmImg($url)
    {
        $db =Yii::$app->db;
        $db->createCommand(' Delete  FROM phototest where url=(:url)', [':url' =>$url, ])->execute();  
        // unlink($url);
        unlink($url);     
    }
     /*
    删除技术article,(根据id删除),删除时会把相应的评论删除
    */
    
    public function rmArt($id)
    {
        $db =Yii::$app->db;
        $db->createCommand(' Delete  FROM article where id=(:id)', [':id' =>$id, ])->execute();  
        $db->createCommand(' Delete  FROM comment where article_id=(:id)', [':id' => $id,])->execute();  
        return true;
    }
     /*
    删除生活文章,(根据id删除),删除时会把相应的评论删除
    */
    public function rmLive($id)
    {
        $db =Yii::$app->db;
        $db->createCommand(' Delete  FROM live where id=(:id)', [':id' => $id,])->execute(); 
        $db->createCommand(' Delete  FROM comment_live where live_id=(:id)', [':id' => $live_id,])->execute();   
        return true;  
    }
      /*
    删除评论,(根据id删除,根据style来删除生活评论或者技术文章评论)
    */
    public function rmComm($id,$style)
    {
         $db =Yii::$app->db;
         if($style=='live')
         {
             $db->createCommand(' Delete  FROM comment_live where comment_id=(:id)',[':id' => $id,])->execute();
             return "live_s";  
         }
         else if($style=='art')
         {
             $db->createCommand(' Delete  FROM comment where comment_id=(:id)', [':id' => $id, ])->execute();
             return "art_s";               
         }
         else
              return false;
    }
       /*
    批量删除技术文章,(根据id删除)$arts的值为接收的要删除的文章id
    */
    public function rmBatchArt($arts)
    {
        $ids="";
        $i=0;
        // $arts中初始长度为1，因为里面有 fm=>arts 用来验证是否是批量删除文章，所以长度少于二则说明无文章id
        if(count($arts)<2)
            return false;
        // 循环删除
        foreach ($arts as $key => $value) {
            if($value=="arts")
                continue;
                $ids[$i]=$value;
                $i++;
            }
            $db =Yii::$app->db;
            for($a=0;$a<count($ids);$a++){
            $db->createCommand(' Delete  FROM article where id =(:id)', [':id' => $ids[$a],])->execute();  
        }
        return true;
    }
    /*
    批量删除live文章,(根据id删除)$lives的值为接收的要删除的文章id
    */
    public function rmBatchLive($lives)
    {
        $ids="";
        $i=0;
        if(count($lives)<2)
            return false;
        foreach ($lives as $key => $value) {
            if($value=="lives")
                  continue;
                  $ids[$i]=$value;
                  $i++;
              }
            $db =Yii::$app->db;
                for($a=0;$a<count($ids);$a++){
                $db->createCommand(' Delete  FROM live where id =(:id)', [
                        ':id' => $ids[$a],
               ])->execute();  
              }
        return true;
    }
     /*
    批量删除live文章评论,(根据id删除)$lives的值为接收的要删除的评论id
    */
    public function rmBatchLiveComm($lives)
    {
        $ids="";
        $i=0;
        if(count($lives)<2)
            return false;
        foreach ($lives as $key => $value) {
            if($value=="comm_lives")
                  continue;
                  $ids[$i]=$value;
                  $i++;
              }
            $db =Yii::$app->db;
            for($a=0;$a<count($ids);$a++){
                $db->createCommand(' Delete  FROM comment_live where comment_id =(:id)', [
                        ':id' => $ids[$a],
               ])->execute();  
            }
        return true;
    }
    /*
    批量删除技术文章评论,(根据id删除)$arts的值为接收的要删除的评论id
    */
    public function rmBatchArtComm($arts)
    {
        $ids="";
        $i=0;
        if(count($arts)<2)
            return false;
        foreach ($arts as $key => $value) {
            if($value=="comms")
                  continue;
                  $ids[$i]=$value;
                  $i++;
              }
            $db =Yii::$app->db;
            for($a=0;$a<count($ids);$a++){
                $db->createCommand(' Delete  FROM comment where comment_id =(:id)', [
                        ':id' => $ids[$a],
               ])->execute();  
            }
        return true;
    }
}


 ?>